/**************************************************************************
	JLog object　	デバッグ用
	Author:		ダン・キエン・タイン
	新規作成：	2007/01/09.
	更新:		2007/01/17.	修正.
				2007/11/08. JLogColor追加.
**************************************************************************/
var JLogColor = Class.create();
JLogColor.prototype = {
	initialize: function(o) {
		this.bkcolor = o.bkcolor || "white";
		this.color	 = o.color || "black";
	},

	toDivOpen: function() {
		return ["<span style='background-color:", this.bkcolor, "; color: ", this.color, ";'>"].join("");
	},

	toDivClose: function() {
		return "</span>";
	}
};
var JColor = {
	YELLOW_RED		: new JLogColor({bkcolor: "yellow", color: "red"}),
	YELLOW_BLUE		: new JLogColor({bkcolor: "yellow", color: "blue"}),
	YELLOW_GREEN	: new JLogColor({bkcolor: "yellow", color: "green"}),
	PINK			: new JLogColor({bkcolor: "pink"}),
	MAGENTA			: new JLogColor({bkcolor: "magenta"}),
	YELLOW			: new JLogColor({bkcolor: "yellow"})
};

var JLog = Class.create();
JLog.prototype = {
	id			: null,
	float_win	: null,
	divDrag		: null,
	divContent	: null,
	mouseX		: null,
	mouseY		: null,
	bLock		: true,
	bDebug		: true,
	aOpacity	: [0.9, 1.0, 0.5, 0.6, 0.7, 0.8],
	nOpacity	: 0,

	initialize: function () {
		this.options = Object.extend({
			width				:	800,
			height				:	150,
			top					:	200,
			left				:	10,
			nInterval			:	500,		// interval in Lock Mode.
			test				:	0
	    }, arguments[0] || {});

	    this.id = "KL1J465SD567F2325548LH25253GLUIWHGLIUHSGDLUHSG";
	    this.write = this.writeHtml;
	},

//==========================================================================
//  Create JLog windows first time.
//	1 instance only.
//==========================================================================
	createWindow: function() {
		if(!this.bDebug) return;

		new Insertion.Bottom($(document.body), "<div id='"+this.id+"'></div>");
		var pos = this.getScrollPosition();

		this.float_win = $(this.id);

		this.float_win.style.position	= "absolute";
		this.float_win.style.top		= (pos.y + this.options.top) + "px";
		this.float_win.style.left		= this.options.left + "px";
		this.float_win.style.border		= "1px solid black";
		this.float_win.style.overflow	= "auto";
		this.float_win.style.textAlign	= "left";
		this.float_win.style.zIndex		= "1";
		$(this.id).setStyle({opacity	: this.aOpacity[this.nOpacity]});

		this.divDrag	= document.createElement("div");
		this.divDrag.id = this.id + "_divDrag";
		this.divDrag.style.padding	= "2px";
		this.divDrag.style.backgroundColor	= "#979797";
		this.divDrag.style.cursor	= "pointer";
		this.divDrag.style.color	= "white";
		this.divDrag.style.fontSize	= "14px";

		var s = [];

		s.push("JLog",
			" - <span id='", this.id, "_clear' onclick='JLog.clear();' style='text-decoration: underline; cursor:pointer;'>Clear</span>",		// Clear button
			" - <span id='", this.id, "_close' onclick='JLog.close();' style='text-decoration: underline; cursor:pointer;'>Close</span>",		// Close button
			" - <span id='", this.id, "_pause' onclick='JLog.pause(this);' style='text-decoration: underline; cursor:pointer;'>Pause</span>",	// Stop/Pause button
			"<!--[if IE]> - <span id='", this.id, "_select' onclick='JLog.select();' style='text-decoration: underline; cursor:pointer;'>Select</span><![endif]-->",		// Select text button. IE only.
			" - <span id='", this.id, "_lock' onclick='JLog.lock(this);' style='text-decoration: underline; cursor:pointer;'>Lock</span>",		// Lock, Unlock button.
			" - <input id='", this.id, "_texteval' id='", this.id, "_text' type='textbox' value='' name='textEval' size='25' style='height:15px;border:1px thin;' onclick='this.focus();'>",	// Eval textbox.
			" &lt;== <span id='", this.id,"_eval' onclick='JLog.eval();' style='text-decoration: underline; cursor:pointer;'>Eval</span>",		// Eval button.
			" <span id='", this.id, "_dump' onclick='JLog._dump();' style='text-decoration: underline; cursor:pointer;'>Dump</span>"				// Dump button.
		);

		this.divDrag.innerHTML	= s.join("");

		this.float_win.appendChild(this.divDrag);

		this.divContent	= document.createElement("div");
		this.divContent.id = this.id + "_content";
		this.divContent.style.backgroundColor	= "white";
		this.divContent.style.color 		= "#333333";
		this.divContent.style.width			= this.options.width + "px";
		this.divContent.style.height		= this.options.height + "px";
		this.divContent.style.overflowX		= "hidden";
		this.divContent.style.overflowY		= "scroll";
		this.divContent.style.fontFamily	= "Verdana, Arial, sans-serif";
		this.divContent.style.fontSize		= "11px";

		this.float_win.appendChild(this.divContent);

		this.divFooter	= document.createElement("div");
		this.divFooter.id = this.id + "_footer";
		this.divFooter.style.padding	= "2px";
		this.divFooter.style.backgroundColor	= "#ECECEC";
		this.divFooter.style.cursor		= "pointer";
		this.divFooter.style.color		= "#999999";
		this.divFooter.style.fontSize	= "14px";
		var s = [];

		s.push(
			"<span id='", this.id, "_opaque' onclick='JLog.opaque();' style='text-decoration: underline; cursor:pointer;'>Opaque</span>",		// Opacity button
			" <span id='", this.id, "_opacityLevel'>", Math.round(this.aOpacity[this.nOpacity]*100) , "%</span>"
		);
		this.divFooter.innerHTML	= s.join("");

		this.float_win.appendChild(this.divFooter);



	    this.eventMouseDown = this.onMouseDown.bindAsEventListener(this);
	    this.eventMouseUp   = this.onMouseUp.bindAsEventListener(this);
	    this.eventMouseMove = this.onMouseMove.bindAsEventListener(this);

	    Event.observe(this.divDrag, "mousedown", this.eventMouseDown);
	},

	opaque: function() {
//		this.nOpacity++;
		this.nOpacity = (this.nOpacity+this.aOpacity.length-1) % this.aOpacity.length;
		$(this.id).setStyle({opacity	: this.aOpacity[this.nOpacity]});
		$(this.id + "_opacityLevel").innerHTML = Math.round(this.aOpacity[this.nOpacity]*100) + "%";
	},

	pause: function(obj) {
		if (this.bDebug) {
			this.bDebug	= false;
			obj.innerHTML = "<font color=red>Resume</font>";
		} else {
			this.bDebug	= true;
			obj.innerHTML = "Pause";
			obj.onclick = function () {JLog.pause(obj);};
		}
	},

	lock: function(obj) {
		if (this.bLock) {
			this.bLock = false;
			obj.innerHTML = "Lock";
			if(this.oInterval) {
				window.clearInterval(this.oInterval);
			}
		} else {
			this.bLock = true;
			obj.innerHTML = "<font color=red>Unlock</font>";
			obj.onclick = function () {JLog.lock(obj);};
			var pos = JLog.getScrollPosition();
			var posWin = Position.cumulativeOffset(this.float_win);
			this.options.left	= posWin[0] - pos.x;
			this.options.top	= posWin[1] - pos.y;
			this.pinIt();
		}
	},

	pinIt: function() {
		this.oInterval = window.setInterval(function() {
			var pos = JLog.getScrollPosition();
			JLog.float_win.style.top		=  (pos.y + JLog.options.top) + "px";
			JLog.float_win.style.left		=  JLog.options.left + "px";
		}, this.options.nInterval);
	},

	writeHtml: function() {
		if(!this.bDebug) return;
		if (!this.divContent) this.createWindow();
		var d = new Date();
		var sH = "000"+d.getHours();
		var sM = "000"+d.getMinutes();
		var sS = "000"+d.getSeconds();
		var sP = "000"+(d.getTime()%1000);
		sH = sH.substring(sH.length-2);
		sM = sM.substring(sM.length-2);
		sS = sS.substring(sS.length-2);
		sP = sP.substring(sP.length-3);

		var s = [];
		s.push("<span style='font-size:9px; padding-right:5px; color:gray;'>", sH, ":", sM, ":", sS, ":", sP, "&nbsp;</span>");
//		s	+="<span>"+sText+"</span>";

		var a = $A(arguments);

		var sDivOpen = "", sDivClose = "";
		if (a[0] instanceof JLogColor) {
			var logColor = a.shift();
			sDivOpen	= logColor.toDivOpen();
			sDivClose	= logColor.toDivClose();
		}

		s.push(sDivOpen);
		var sText = [];
		if (a.length>0) {
			for(var i=0; i<a.length; i++) {
				sText.push(a[i]);
			}
		}

		s.push(sText.join(", "));

		var o = document.createElement("div");
		o.style.borderBottom	= "1px solid #AAAAAA";
		o.style.paddingBottom	= "2px";
		o.innerHTML = s.join("");
		this.divContent.appendChild(o);
		this.divContent.scrollTop = this.divContent.scrollHeight;
	},

	select: function() {
 		var r = document.body.createTextRange();
 		r.moveToElementText(this.divContent);
 		r.select();
	},

	eval: function() {
		var o = $(""+this.id+"_texteval");
		try {
			JLog.writeHtml("<span style='background-color:yellow;'>Evaluate: </span>" + o.value.escapeHTML());
			var r = eval(o.value);
			if (r != null) JLog.writeHtml(r);
		} catch (err) {
			JLog.writeHtml("<span style='background-color:yellow; color:red;'>ERROR: <b>"+err+"</b></span>");
		}
	},

	dump: function(o) {
		try {
			var a = ["<br>{<br>"];

			for(var i in o) {
				if (!o.hasOwnProperty ||o.hasOwnProperty(i)) {
					a.push("　", i, ": ", o[i], "<br>");
				}
			}
			a.push("}");
			this.writeHtml(a.join(""));
		} catch (err) {
			JLog.writeHtml("<span style='background-color:yellow; color:red;'>ERROR: <b>"+err+"</b></span>");
		}
	},

	_dump: function() {
		var o = eval($(""+this.id+"_texteval").value);
		this.dump(o);
	},

	clear: function() {
		this.divContent.innerHTML = "";
	},

	close: function() {
		this.float_win.style.display = "none";
	},

	open: function () {
		this.float_win.style.display = "block";
	},

	moveXY: function(x, y) {
		this.float_win.style.left	= x + "px";
		this.float_win.style.top	= y + "px";
	},

	resize: function(width, height) {
		if (width!=0) this.options.width	= width;
		if (height!=0) this.options.height	= height;

		this.divContent.style.width			= this.options.width + "px";
		this.divContent.style.height		= this.options.height + "px";
	},


	onMouseDown: function(e) {
		this.mouseX = Event.pointerX(e);
		this.mouseY = Event.pointerY(e);

		Event.observe(document, "mouseup", this.eventMouseUp, false);
		Event.observe(document, "mousemove", this.eventMouseMove, false);
		Event.stop(e);
	},

	onMouseUp: function(e) {
	    Event.stopObserving(document, "mouseup", this.eventMouseUp, false);
	    Event.stopObserving(document, "mousemove", this.eventMouseMove, false);
	    Event.stop(e);
	},

	onMouseMove: function(e) {
		var mouseX = Event.pointerX(e);
		var mouseY = Event.pointerY(e);

		dX = mouseX - this.mouseX;
		dY = mouseY - this.mouseY;

		this.mouseX = mouseX;
		this.mouseY = mouseY;

		this.moveXY(parseInt(this.float_win.style.left) + dX, parseInt(this.float_win.style.top) + dY);
		Event.stop(e);
	},

	getScrollPosition: function() {
		var win = window, body = document.body, html = document.documentElement;
		return {
			"x": ((win && win.pageXOffset) || (html && html.scrollLeft) || (body && body.scrollLeft) || 0),
			"y": ((win && win.pageYOffset) || (html && html.scrollTop) || (body && body.scrollTop) || 0)
		};
	}
};

JLog = new JLog();